package view;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

/**
 * Email preview area on the bottom
 * 
 * @author Farzana Alam (farzana22@gmail.com)
 * @since May, 2013
 * @version $Revision: 1.0 $
 */
@SuppressWarnings("serial")
public class PreviewView extends JPanel {
	
	// Display fields to be set from external controller
	private JTextField to, from, date, subject;
	
	// Body to be set from external
	private JTextArea body;
	
	// Date format to be displayed
	private String dateFormat;
	
	/**
	 * constructor 
	 * 
	
	 * @param migLayout migLayout the used layout for the panel
	 */
	public PreviewView(MigLayout migLayout) {

		// Calling super constructor
		super(migLayout);
		
		// Set date display format
		dateFormat = "yyyy.MMMMM.dd hh:mm aaa";
		
		setupComponents();
	}
	
	/**
	 * Setup components in this panel
	 */
	private void setupComponents(){

		// Initialize
		date = new JTextField();
		from = new JTextField();
		to = new JTextField();
		subject = new JTextField();
		body = new JTextArea();
		JScrollPane bodyScroll = new JScrollPane(body);
		
		// Set properties
		date.setEditable(false);
		from.setEditable(false);
		to.setEditable(false);
		subject.setEditable(false);
		body.setEditable(false);
		
		// add
		this.add(new JLabel("From:"));
		this.add(from, "wrap, growx");
		this.add(new JLabel("To:"));
		this.add(to, "wrap, push, growx");
		this.add(new JLabel("Date:"));
		this.add(date, "wrap, growx");
		this.add(new JLabel("Subject:"));
		this.add(subject, "wrap, growx");
		this.add(bodyScroll, "span 2, growx, h 100:400:");
	}

	/**
	 * Sets the To field
	 * 
	
	 * @param toText String
	 */
	public void setTo(String toText) {
		this.to.setText(toText);
		
	}
	
	/**
	 * Sets the From field
	 * 
	
	 * @param fromText String
	 */
	public void setFrom(String fromText) {
		this.from.setText(fromText);
	}
	
	/**
	 * Sets the date field
	 * 
	
	 * @param timestamp timestamp the timestamp of the message
	 */
	public void setDate(Timestamp timestamp) {
		String d = new SimpleDateFormat(dateFormat).format(timestamp);
		this.date.setText(d);
	}
	
	/**
	 * Sets the subject field
	 * 
	
	 * @param subjectText String
	 */
	public void setSubject(String subjectText) {
		this.subject.setText(subjectText);
	}
	
	/**
	 * Gets the text data from the "body" text field
	 *  
	
	 * @return String the message text body */
	public String getBodyText() {
	    return this.body.getText();
	}
	/**
	 * Sets text for the "body" text field
	 * 
	
	 * @param body String
	 */
	public void setBodyText(String body)
	{
		this.body.setText(body);
	}
	/**
	 * Gets text from "date" text field
	 * 
	
	 * @return String the date of the message  */
	public String getDateText()
	{
		return this.date.getText();
	}
	/**
	 * Sets text for "date" text field
	 * 
	
	 * @param date String
	 */
	public void setDateText(String date)
	{
		this.date.setText(date);
	}
	/**
	 * Gets text from "from" text field
	 * 
	
	 * @return String the text contained in the "From" field */
	public String getFromText()
	{
		return this.from.getText();
	}
	/**
	 * Sets text for the "from" text field
	 * 
	
	 * @param from String
	 */
	public void setFromText(String from)
	{
		this.from.setText(from);
	}
	/**
	 * Gets text from "subject" text field
	 * 
	
	 * @return String the text of the "Subject" field */
	public String getSubjectText()
	{
		return this.subject.getText();
	}
	/**
	 * Sets text for "subject" text field
	 * 
	
	 * @param subject String
	 */
	public void setSubjectText(String subject)
	{
		this.subject.setText(subject);
	}
	/**
	 * Get text from "to" text field
	 * 
	
	 * @return String returns the address of the recipient */
	public String getToText()
	{
		return this.to.getText();
	}
	/**
	 * Sets text for "to" text field
	 * 
	
	 * @param to String
	 */
	public void setToText(String to)
	{
		this.to.setText(to);
	}

	/**
	 * Clears the preview view emptying all fields
	 */
	public void clear() {
		to.setText("");
		from.setText("");
		subject.setText("");
		body.setText("");
		date.setText("");
	}

}
